package logic

import (
	"context"
	"encoding/json"
	"lm/cache"
	"lm/models/mysql"
	"time"

	"lm/lmrpc/internal/svc"
	"lm/lmrpc/lmrpc"

	"github.com/zeromicro/go-zero/core/logx"
)

type LmShowLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewLmShowLogic(ctx context.Context, svcCtx *svc.ServiceContext) *LmShowLogic {
	return &LmShowLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

func (l *LmShowLogic) LmShow(in *lmrpc.LmShowRequest) (*lmrpc.LmShowResponse, error) {
	// todo: add your logic here and delete this line
	var lm mysql.Lm
	mysql.DB.Debug().Find(&lm)

	marshal, _ := json.Marshal(&lm)
	cache.Rdb.Set(l.ctx, "lmShow", marshal, time.Hour*1)

	return &lmrpc.LmShowResponse{
		Title:   lm.Title,
		Image:   lm.Image,
		Text:    lm.Text,
		Price:   float32(lm.Price),
		ClassId: int64(lm.ClassId),
		BrandId: int64(lm.BrandId),
	}, nil
}
